import React from 'react';
class Index extends React.Component {
	constructor() {
		super()
		this.state = {
			arr: [],
			gwc: []
		}
	}
	componentDidMount() {
		var that = this;
		if (window.localStorage.gwc) {
			this.setState({
				gwc: JSON.parse(window.localStorage.gwc)
			})
		}
		fetch("http://www.myhm.xyz/food.php?page=1").then(function(res) {
			res.json().then(function(res) {
				console.log(res)
				that.setState({
					arr: res.result.list
				})
			})
		})
	}
	getIndex(ind) {
		var gwc = this.state.gwc
		var arr = this.state.arr
		if (gwc.length === 0) {
			ind.shu = 1
			ind.xuank = 1
			gwc.push(ind)
			window.localStorage.gwc = JSON.stringify(this.state.gwc)
			console.log(JSON.parse(window.localStorage.gwc))
		} else {
			for (let i = 0; i < arr.length; i++) {
				if (arr[i].id === ind.id) {
					for (let j = 0; j < gwc.length; j++) {
						if (gwc[j].id === ind.id) {
							gwc[j].shu++
							this.setState({
								gwc: gwc
							})
							window.localStorage.gwc = JSON.stringify(this.state.gwc)
							console.log(JSON.parse(window.localStorage.gwc))
							return false
						}
					}
					ind.shu = 1
					ind.xuank = 1
					gwc.push(ind)
					this.setState({
						gwc: gwc
					})
					window.localStorage.gwc = JSON.stringify(this.state.gwc)
					console.log(JSON.parse(window.localStorage.gwc))
					return false
				}
			}
		}
	}
	render() {
		var arr = this.state.arr;
		var arr1 = arr.map((key, index) => {
			return ( <
				li className = "showLi"
				key = {
					index
				} >
				<
				img src = {
					key.thumb
				}
				/> <
				div className = "price_list" >
				<
				div > {
					key.title
				} < /div> <
				div className = "price" >
				<
				span className = "money" > {
					key.productprice
				} < /span> <
				span className = "add"
				onClick = {
					this.getIndex.bind(this, key)
<<<<<<< HEAD
				} >+</span >
=======
				} >
				<
				span className = "jia" > + < /span></span >
>>>>>>> 02b69c8bc3d30aaaa94bbd11a54f16d3ad48afa6
				<
				/div> < /
				div > <
				/li>
			)
		})
		return ( <
			div >
			<
			ul > {
				arr1
			} <
			/ul> < /
			div >
		)
	}
}
export default Index;
